*Insheet and process JOLTS researcher data
cd /OEUS/users/FEDSusers/JOLTS
set more off

use ./data/JOLTS_ntilde, clear

* Set weighting
*g wghtvar = 1
*local wght = "unwt"
local wght = "wt"
g wghtvar = emp 
gen weight_emp = wghtvar*weight_f



* Grab size bins based on pooled sample:
cap log close
log using "/OEUS/users/FEDSusers/JOLTS/rnd2final/`wght'SizeBins_rnd2final_2.txt", replace
sum emp if emp!=. & emp>0 [aw=weight_emp]
g sz5_emp = r(max)
g sz0_emp = 0
*
pctile szpct = emp if emp!=. & emp>0 [aw=weight_emp], nq(5)
list szpct in 1/4
forval j=1(1)4 {
	g xsz`j'_emp = 0 
	replace xsz`j'_emp = szpct if _n==`j'
	egen sz`j'_emp = max(xsz`j'_emp)
}
drop xsz*
/*
g sz1_emp = r(p25)
g sz2_emp = r(p50)
g sz3_emp = r(p75)
*/
log close
*
/* Check size bins
g flag0 = (0<emp)*(emp!=.)
g flag1 = (emp<=19)*flag0
g flag2 = (emp> 19)*(emp <=64)*flag0
egen totemp0 = sum(weight_f*emp*flag0)
egen totemp1 = sum(weight_f*emp*flag1)
egen totemp2 = sum(weight_f*emp*flag2)
g shremp1 = totemp1 / totemp0 
g shremp2 = totemp2 / totemp0
*/



* Make Inaction indicators:
gen flag0 = 1
gen flag11= 1
gen flag22= 1
*
qui foreach i of numlist 1/24{
	gen inactalt0_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt0_`i' = 1 if emp == ntilde`i'  & inactalt0_`i' == 0
	*
	gen allq_inactalt0_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace allq_inactalt0_`i' = 1 if emp == ntilde`i'  & allq_inactalt0_`i' == 0 & flag0==1
	replace flag0 = 0 if allq_inactalt0_`i'==0 | allq_inactalt0_`i'==.	
}
/* NOTE - Check inaction rates:
sum inactalt0_1 [aw=weight_f]
sum inactalt0_2 [aw=weight_f]
sum inactalt0_3 [aw=weight_f]
sum allq_inactalt0_1 [aw=weight_f]
sum allq_inactalt0_2 [aw=weight_f]
sum allq_inactalt0_3 [aw=weight_f]
*/

*Define quarterly inaction with a window as maximum of emp and percentage
*max(1,1%)
qui foreach i of numlist 1/24{
	gen inactalt11_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt11_`i' = 1 if emp >= min(floor(.99*ntilde`i'),ntilde`i'-1) & emp<=max(ceil(1.01*ntilde`i'),ntilde`i'+1) & inactalt11_`i' == 0
	*
	gen allq_inactalt11_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace allq_inactalt11_`i' = 1 if emp >= min(floor(.99*ntilde`i'),ntilde`i'-1) & emp<=max(ceil(1.01*ntilde`i'),ntilde`i'+1) & allq_inactalt11_`i' == 0 & flag11==1
	replace flag11 = 0 if allq_inactalt11_`i'==0 | allq_inactalt11_`i'==.
}
/* NOTE - Check inaction rates:
sum inactalt11_1 [aw=weight_f]
sum inactalt11_2 [aw=weight_f]
sum inactalt11_3 [aw=weight_f]
sum allq_inactalt11_1 [aw=weight_f]
sum allq_inactalt11_2 [aw=weight_f]
sum allq_inactalt11_3 [aw=weight_f]
*/

*max(2,2%)
qui foreach i of numlist 1/24{
	gen inactalt22_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace inactalt22_`i' = 1 if emp >= min(floor(.98*ntilde`i'),ntilde`i'-2) & emp<=max(ceil(1.02*ntilde`i'),ntilde`i'+2) & inactalt22_`i' == 0
	*
	gen allq_inactalt22_`i' = 0 if emp!=. & ntilde`i'!=. & emp>0 & ntilde`i' >0 
	replace allq_inactalt22_`i' = 1 if emp >= min(floor(.98*ntilde`i'),ntilde`i'-2) & emp<=max(ceil(1.02*ntilde`i'),ntilde`i'+2) & allq_inactalt22_`i' == 0 & flag22==1
	replace flag22 = 0 if allq_inactalt22_`i'==0 | allq_inactalt22_`i'==.
}


foreach w in 0 11 22 { 
	disp `w'

	preserve
	gen inactrate`w' = .
	gen avehires=.
	gen avequits=.
	gen aveothsep = .
	gen averhires`w'=.
	gen averquits`w'=. 
	gen averothsep`w' = .
	gen avesize`w' = .
	gen count`w' = .
	gen avetotsep`w' =.
	gen avertotsep`w' = .

	*
	qui foreach var in allq sz1 sz2 sz3 sz4 sz5 {
		gen `var'_inactrate`w' = .
		gen `var'_avehires=.
		gen `var'_avequits=.
		gen `var'_aveothsep = .
		gen `var'_averhires`w'=.
		gen `var'_averquits`w'=. 
		gen `var'_averothsep`w' = .
		gen `var'_avesize`w' = .
		gen `var'_count`w' = .
		gen `var'_avetotsep`w' = .
		gen `var'_avertotsep`w' = .
	}
	*
	qui foreach var in sz1 sz2 sz3 sz4 sz5 {
		gen `var'_allq_inactrate`w' = .
		gen `var'_allq_avehires=.
		gen `var'_allq_avequits=.
		gen `var'_allq_aveothsep = .
		gen `var'_allq_averhires`w'=.
		gen `var'_allq_averquits`w'=. 
		gen `var'_allq_averothsep`w' = .
		gen `var'_allq_avesize`w' = .
		gen `var'_allq_count`w' = .
		gen `var'_allq_avetotsep`w' = .
		gen `var'_allq_avertotsep`w' = .
	}
			


	foreach i of numlist 1/24{
	
		qui{
		
		** BASELINE: INACTION MEANS NO NET CHANGE **
		sum inactalt`w'_`i' [aw=weight_f]
		replace inactrate`w' = r(mean) if _n==`i'
		
		sum ntilde`i' [aw=weight_f] if inactalt`w'_`i'==1 
		replace avesize`w' = r(mean) if _n==`i'
		replace count`w' = r(N) if _n==`i'
	
		foreach var in hires quits othsep totsep {
			sum `var'`i' [aw=weight_f] if inactalt`w'_`i'==1 
			replace ave`var' = r(mean) if _n==`i'
			replace aver`var'`w' = ave`var'/avesize`w' if _n==`i'	/* Use E[flow]/E[n] */
			
		}
		*replace count = r(N) if _n==`i'
		
		
		* Baseline Inaction by size bin
	
		forval j = 1(1)5 {  /*Size Bin*/
			local jj = `j' -1
			sum inactalt`w'_`i' [aw=weight_f] if  sz`jj'_emp< ntilde`i' & ntilde`i' <=sz`j'_emp 
			replace sz`j'_inactrate`w' = r(mean) if _n==`i'
			
			sum ntilde`i' [aw=weight_f] if inactalt`w'_`i'==1  &  sz`jj'_emp< ntilde`i' & ntilde`i' <=sz`j'_emp 
			replace sz`j'_avesize`w' = r(mean) if _n==`i'
			replace sz`j'_count`w' = r(N) if _n==`i'
			
			foreach var in hires quits othsep totsep {
				sum `var'`i' [aw=weight_f] if inactalt`w'_`i'==1 &  sz`jj'_emp< ntilde`i' & ntilde`i' <=sz`j'_emp
				replace sz`j'_ave`var' = r(mean) if _n==`i'
				replace sz`j'_aver`var'`w' = sz`j'_ave`var'/sz`j'_avesize`w' if _n==`i'
				
			}
		}
		
		
		
		** ALTERNATIVE: INACTION MEANS INACTIVE IN ALL QTS **

		sum allq_inactalt`w'_`i' [aw=weight_f]
		replace allq_inactrate`w' = r(mean) if _n==`i'		
		
		sum ntilde`i' [aw=weight_f] if allq_inactalt`w'_`i'==1 
		replace allq_avesize`w' = r(mean) if _n==`i'
	        replace allq_count`w' = r(N) if _n==`i'
	
		foreach var in hires quits othsep totsep {
			sum `var'`i' [aw=weight_f] if allq_inactalt`w'_`i'==1 
			replace allq_ave`var' = r(mean) if _n==`i'
			replace allq_aver`var'`w'  = allq_ave`var'/allq_avesize`w'  if _n==`i'
			
		}
		
		
		*All-qt Inaction by size bin
		forval j = 1(1)5 {  /*Size Bin*/
			local jj = `j' -1
			sum allq_inactalt`w'_`i' [aw=weight_f] if  sz`jj'_emp< ntilde`i' & ntilde`i' <=sz`j'_emp 
			replace sz`j'_allq_inactrate`w' = r(mean) if _n==`i'
			
			sum ntilde`i' [aw=weight_f] if allq_inactalt`w'_`i'==1  &  sz`jj'_emp< ntilde`i' & ntilde`i' <=sz`j'_emp 
			replace sz`j'_allq_avesize`w' = r(mean) if _n==`i'
			replace sz`j'_allq_count`w' = r(N) if _n==`i'
		
			foreach var in hires quits othsep totsep {
				sum `var'`i' [aw=weight_f] if allq_inactalt`w'_`i'==1 &  sz`jj'_emp< ntilde`i' & ntilde`i' <=sz`j'_emp
				replace sz`j'_allq_ave`var' = r(mean) if _n==`i'
				replace sz`j'_allq_aver`var'`w' = sz`j'_allq_ave`var'/sz`j'_allq_avesize`w' if _n==`i'
				
			}
		}
		
		}	   /*End quietly*/
	
	}	/*End loop over duration*/
		
	*gen obsnum = _n
	*sum count
	keep obsnum inactrate`w' allq_inactrate`w' ///
		    sz1_inactrate`w' sz2_inactrate`w' sz3_inactrate`w' sz4_inactrate`w' sz5_inactrate`w' ///
		    sz1_allq_inactrate`w'  sz2_allq_inactrate`w'  sz3_allq_inactrate`w'  sz4_allq_inactrate`w'  sz5_allq_inactrate`w' ///
		    averhires`w' averquits`w' averothsep`w' avesize`w' avertotsep`w' ///
		    allq_averhires`w'  allq_averquits`w'  allq_averothsep`w'  allq_avesize`w' allq_avertotsep`w' ///
		    sz1_averhires`w'  sz2_averhires`w'  sz3_averhires`w'  sz4_averhires`w' sz5_averhires`w' ///
		    sz1_averquits`w'  sz2_averquits`w'  sz3_averquits`w'  sz4_averquits`w' sz5_averquits`w' ///
		    sz1_averothsep`w' sz2_averothsep`w' sz3_averothsep`w' sz4_averothsep`w' sz5_averothsep`w' ///
		    sz1_avesize`w'  sz2_avesize`w'  sz3_avesize`w'  sz4_avesize`w' sz5_avesize`w' ///
		    sz1_avertotsep`w'  sz2_avertotsep`w'  sz3_avertotsep`w'  sz4_avertotsep`w' sz5_avertotsep`w' ///		    
		    sz1_allq_averhires`w'  sz2_allq_averhires`w'  sz3_allq_averhires`w'  sz4_allq_averhires`w'  sz5_allq_averhires`w' ///
		    sz1_allq_averquits`w'  sz2_allq_averquits`w'  sz3_allq_averquits`w'  sz4_allq_averquits`w'  sz5_allq_averquits`w' ///
		    sz1_allq_averothsep`w' sz2_allq_averothsep`w' sz3_allq_averothsep`w' sz4_allq_averothsep`w' sz5_allq_averothsep`w' ///
		    sz1_allq_avesize`w'  sz2_allq_avesize`w'  sz3_allq_avesize`w'  sz4_allq_avesize`w'  sz5_allq_avesize`w' ///
		    sz1_allq_avertotsep`w' sz2_allq_avertotsep`w' sz3_allq_avertotsep`w' sz4_allq_avertotsep`w' sz5_allq_avertotsep`w' ///
		    count`w' sz1_count`w'  sz2_count`w'  sz3_count`w'  sz4_count`w' sz5_count`w'  ///
		    allq_count`w' sz1_allq_count`w' sz2_allq_count`w'  sz3_allq_count`w'  sz4_allq_count`w' sz5_allq_count`w' 
	     
	keep if obsnum<=24

	* Aggregate over size bins
	gen szadj_averhires`w'  = (1/5)*(sz1_averhires`w' + sz2_averhires`w' + sz3_averhires`w' + sz4_averhires`w' + sz5_averhires`w')  
	gen szadj_averquits`w'  = (1/5)*(sz1_averquits`w' + sz2_averquits`w' + sz3_averquits`w' + sz4_averquits`w' + sz5_averquits`w')  
	gen szadj_averothsep`w' = (1/5)*(sz1_averothsep`w' + sz2_averothsep`w' + sz3_averothsep`w' + sz4_averothsep`w'+ sz5_averothsep`w')  
	gen szadj_count`w'      = sz1_count`w' + sz2_count`w' + sz3_count`w' + sz4_count`w'+ sz5_count`w'
	gen szadj_avertotsep`w' = (1/5)*(sz1_avertotsep`w' + sz2_avertotsep`w' + sz3_avertotsep`w' + sz4_avertotsep`w'+ sz5_avertotsep`w')  
	
	gen szadj_allq_averhires`w'  = (1/5)*(sz1_allq_averhires`w' + sz2_allq_averhires`w' + sz3_allq_averhires`w' + sz4_allq_averhires`w' + sz5_allq_averhires`w')  
	gen szadj_allq_averquits`w'  = (1/5)*(sz1_allq_averquits`w' + sz2_allq_averquits`w' + sz3_allq_averquits`w' + sz4_allq_averquits`w' + sz5_allq_averquits`w')  
	gen szadj_allq_averothsep`w' = (1/5)*(sz1_allq_averothsep`w' + sz2_allq_averothsep`w' + sz3_allq_averothsep`w' + sz4_allq_averothsep`w' + sz5_allq_averothsep`w') 
	gen szadj_allq_count`w'      = sz1_allq_count`w' + sz2_allq_count`w' + sz3_allq_count`w' + sz4_allq_count`w' + sz5_allq_count`w'
	gen szadj_allq_avertotsep`w' = (1/5)*(sz1_allq_avertotsep`w' + sz2_allq_avertotsep`w' + sz3_allq_avertotsep`w' + sz4_allq_avertotsep`w' + sz5_allq_avertotsep`w') 
	
	
	
	sort obsnum
	if `w' ==0 {
		save `wght'cumulflows_20240531, replace
	}
	else {
		merge 1:1 obsnum using `wght'cumulflows_20240531 
		drop _merge 
		save `wght'cumulflows_20240531, replace
	}
	
	restore
}	
*
* Divide up for disclosure and suppress cells
use `wght'cumulflows_20240531, clear
keep obsnum averhires* averquits* averothsep* avertotsep* avesize* count* inactrate*
foreach var in averhires averquits averothsep avertotsep avesize inactrate{
	foreach w in 0 11 22{
		replace `var'`w' = . if count`w'<100
}
}

outsheet using "/OEUS/users/FEDSusers/JOLTS/rnd2final/`wght'cumulflows_net_rnd2final_2.csv", replace 

use `wght'cumulflows_20240531, clear
keep obsnum allq_averhires* allq_averquits* allq_averothsep* allq_avertotsep* allq_avesize* allq_count* allq_inactrate*
foreach var in allq_averhires allq_averquits allq_averothsep allq_avertotsep allq_avesize  allq_inactrate{
		foreach w in 0 11 22{
			replace `var'`w' = . if allq_count`w'<100
}
}

outsheet using "/OEUS/users/FEDSusers/JOLTS/rnd2final/`wght'cumulflows_allq_rnd2final_2.csv", replace 

use `wght'cumulflows_20240531, clear
keep obsnum szadj_averhires* szadj_averquits* szadj_averothsep* szadj_avertotsep* szadj_count* 
foreach var in szadj_averhires szadj_averquits szadj_averothsep szadj_avertotsep{
	foreach w in 0 11 22{
		replace `var'`w' = . if szadj_count`w'<100
	
}
}
outsheet using "/OEUS/users/FEDSusers/JOLTS/rnd2final/`wght'cumulflows_sznet_rnd2final_2.csv", replace 

use `wght'cumulflows_20240531, clear
keep obsnum szadj_allq_averhires* szadj_allq_averquits* szadj_allq_averothsep* szadj_allq_avertotsep* szadj_allq_count* 
foreach var in szadj_allq_averhires szadj_allq_averquits szadj_allq_averothsep szadj_allq_avertotsep{
	
	foreach w in 0 11 22{
		replace `var'`w' = . if szadj_allq_count`w'<100	
	}
	
}
outsheet using "/OEUS/users/FEDSusers/JOLTS/rnd2final/`wght'cumulflows_szallq_rnd2final_2.csv", replace 

use `wght'cumulflows_20240531, clear
keep obsnum sz1_averhires* sz1_averquits* sz1_averothsep* sz1_avertotsep* sz1_avesize*  sz1_count*  sz1_inactrate* ///
sz2_averhires* sz2_averquits* sz2_averothsep* sz2_avertotsep* sz2_avesize*  sz2_count*  sz2_inactrate* ///
sz3_averhires* sz3_averquits* sz3_averothsep* sz3_avertotsep* sz3_avesize*  sz3_count*  sz3_inactrate* ///
sz4_averhires* sz4_averquits* sz4_averothsep* sz4_avertotsep* sz4_avesize*  sz4_count*  sz4_inactrate* ///
sz5_averhires* sz5_averquits* sz5_averothsep* sz5_avertotsep* sz5_avesize*  sz5_count*  sz5_inactrate*

foreach var in averhires averquits averothsep avertotsep avesize inactrate{
	
	forvalues j = 1/5{
		foreach w in 0 11 22{
			replace sz`j'_`var'`w' = . if sz`j'_count`w'<100
			
		}
		
	}
} 

outsheet using "/OEUS/users/FEDSusers/JOLTS/rnd2final/`wght'cumulflows_szbinnet_rnd2final_2.csv", replace 

use `wght'cumulflows_20240531, clear
keep obsnum sz1_allq_averhires* sz1_allq_averquits* sz1_allq_averothsep* sz1_allq_avertotsep* sz1_allq_avesize*  sz1_allq_count*  sz1_allq_inactrate* ///
sz2_allq_averhires* sz2_allq_averquits* sz2_allq_averothsep* sz2_allq_avertotsep* sz2_allq_avesize*  sz2_allq_count*  sz2_allq_inactrate*  ///
sz3_allq_averhires* sz3_allq_averquits* sz3_allq_averothsep* sz3_allq_avertotsep* sz3_allq_avesize*  sz3_allq_count*  sz3_allq_inactrate*  ///
sz4_allq_averhires* sz4_allq_averquits* sz4_allq_averothsep* sz4_allq_avertotsep* sz4_allq_avesize*  sz4_allq_count*  sz4_allq_inactrate*  ///
sz5_allq_averhires* sz5_allq_averquits* sz5_allq_averothsep* sz5_allq_avertotsep* sz5_allq_avesize*  sz5_allq_count*  sz5_allq_inactrate*


foreach var in averhires averquits averothsep avertotsep avesize inactrate{
	
	forvalues j = 1/5{
		foreach w in 0 11 22{
			replace sz`j'_allq_`var'`w' = . if sz`j'_allq_count`w'<100
			
		}
		
	}
} 

outsheet using "/OEUS/users/FEDSusers/JOLTS/rnd2final/`wght'cumulflows_szbinallq_rnd2final_2.csv", replace 


/*
Check if any count is < 20. The size bin counts 
for all quarters EXACT inaction are the ones 
that will fall below 20 if any do:

sum sz1_allq_count0
sum sz2_allq_count0
sum sz3_allq_count0
sum sz4_allq_count0 
sum sz5_allq_count0
*/	
	




